﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.Model;
using YouHP.BLL;

namespace YouHP.Pages.Admin
{
    public class EditDisease : YouHP.Pages.BasePage
    {

        protected DiseaseInfo DszInfo { get; set; }

        protected override void ShowPage()
        {
            base.ShowPage();

            if (IsPost)
            {
                DoPost();
            }
            else
            {
                DoGet();
            }
        }

        private void DoGet()
        {
            int did = RequestTools.GetQueryInt("did", -1);
            if (did <= 0)
            {
                AddErrLine("参数错误");
            }
            DszInfo = DiseaseBLL.Instance.GetDiseaseInfo(did);
            if (DszInfo == null || DszInfo.Id != did)
            {
                AddErrLine("疾病不存在");
            }
            if (this.ErrorCount > 0)
            {
                //this.SetMetaRefresh(2, "AddDisease.aspx");
                this.SetShowBackLink(true);
                return;
            }
        }

        protected void DoPost()
        {
            int dszid = RequestTools.GetFormInt("dszid",-1);
            string dszname = RequestTools.GetFormString("dszname");
            string dszintro = RequestTools.GetFormString("dszintro");
            string dszdesp = RequestTools.GetFormString("dszdesp");
            if (dszid <= 0)
            {
                AddErrLine("参数错误");
            }
            if (dszname == "" || dszname.Length < 2)
            {
                AddErrLine("疾病名称长度不能小于 2 ");
            }
            if (dszintro == "" || dszintro.Length < 2)
            {
                AddErrLine("疾病简介长度不能小于 2 ");
            }
            if (dszdesp == "" || dszdesp.Length < 2)
            {
                AddErrLine("疾病描述长度不能小于 2 ");
            }
            if (this.ErrorCount > 0)
            {
                //this.SetMetaRefresh(2, "AddDisease.aspx");
                this.SetShowBackLink(true);
                return;
            }

            DiseaseInfo info = new DiseaseInfo();
            info.Id = dszid;
            info.Name = dszname;
            info.Intro = dszintro;
            info.Desp = dszdesp;
            info.PinYin = Chinese.GetPingYinFirstLetter(info.Name);
            info.Type = 1;
            info.UpdateTime = DateTime.Now;
            DiseaseBLL.Instance.UpdateDisease(info);
            this.AddMsgLine("疾病修改成功");
            this.SetUrl("ListDiseases.aspx");
            this.SetMetaRefresh(3, "ListDiseases.aspx");

        }
    }
}
